package me.mouse.gokistats.util;

import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JavaScript {

	private ScriptEngine engine;
	private String File;
	private Invocable inv;

	public JavaScript(String File) throws FileNotFoundException,
			ScriptException {
		ScriptEngineManager engineManager = new ScriptEngineManager();
		engine = engineManager.getEngineByName("JavaScript");
		this.File = File;
		engine.eval(new FileReader(File));
		inv = (Invocable) engine;
	}

	public JavaScript(JavaScript formula) {
		File = formula.getFile();
		engine = formula.getScriptEngine();
		inv = formula.getInvocable();
	}

	public ScriptEngine getScriptEngine() {
		return engine;
	}

	public String getFile() {
		return File;
	}

	public Invocable getInvocable() {
		return inv;
	}

	public Object invokeFunction(String arg0, Object... arg1)
			throws NoSuchMethodException, ScriptException {
		return inv.invokeFunction(arg0, arg1);
	}

	public Object invokeMethod(Object thiz, String name, Object... args)
			throws NoSuchMethodException, ScriptException {
		return inv.invokeMethod(thiz, name, args);
	}
}
